package com.rm.zeyou.util.sms.impl;// package com.rm.chengjiaodaojia.util.sms.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.rm.zeyou.dto.AliyunSendSmsDTO;
import com.rm.zeyou.mapper.SmsTemplateConfigMapper;
import com.rm.zeyou.util.sms.SendSmsService;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 */
@Data
@Log4j2
@Service("aliyunSendSms")
public class AliyunSendSms implements SendSmsService {
    private String templateCode;
    //   dysmsapi.aliyuncs.com # 固定不变
//    private String domain;
    // 指定地域名称 短信API的就是 cn-hangzhou 不能改变
//    private String regionId;
    @Resource
    private SmsTemplateConfigMapper smsTemplateConfigMapper;

    /**
     * 阿里云发送验证码
     *
     * @param tel
     * @param code
     * @return
     */
    @Override
    public String SendSms(String tel, String code) throws Exception {
        String accessKeyId = smsTemplateConfigMapper.selectOneData("accessKeyId");
        String accessKeySecret = smsTemplateConfigMapper.selectOneData("accessKeySecret");
        String signName = smsTemplateConfigMapper.selectOneData("signName");
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = AliyunSendSms.createClient(accessKeyId, accessKeySecret);
        AliyunSendSmsDTO aliyunSendSmsDTO = new AliyunSendSmsDTO();
        aliyunSendSmsDTO.setCode(code);
        log.info("发送验证码参数：signName：[{}]，templateCode：[{}]，tel：[{}]，templateParam：[{}]", signName, templateCode, tel, JSON.toJSONString(aliyunSendSmsDTO, true));
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(tel)
                .setTemplateParam(JSON.toJSONString(aliyunSendSmsDTO));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return code;
    }


    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


    /**
     * 发送短信接口
     *
     * @param phoneNum 手机号
     * @param message  消息
     * @return
     */
    @Override
    public Integer sendSms(String phoneNum, String message, String templateCode) throws Exception {
        String accessKeyId = smsTemplateConfigMapper.selectOneData("accessKeyId");
        String accessKeySecret = smsTemplateConfigMapper.selectOneData("accessKeySecret");
        String signName = smsTemplateConfigMapper.selectOneData("signName");
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = AliyunSendSms.createClient(accessKeyId, accessKeySecret);
        AliyunSendSmsDTO aliyunSendSmsDTO = new AliyunSendSmsDTO();
        aliyunSendSmsDTO.setCode(message);
        log.info("发送验证码参数：signName：[{}]，templateCode：[{}]，tel：[{}]，templateParam：[{}]", signName, templateCode, phoneNum, JSON.toJSONString(aliyunSendSmsDTO, true));
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phoneNum)
                .setTemplateParam(JSON.toJSONString(aliyunSendSmsDTO));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            return sendSmsResponse.getStatusCode();
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            log.warn(error.message);
//            Common.assertAsString(error.message);
        }
        return 0;
    }

    @Override
    public Integer orderSendSms(String phoneNum, String name, String templateCode) throws Exception {
        String accessKeyId = smsTemplateConfigMapper.selectOneData("accessKeyId");
        String accessKeySecret = smsTemplateConfigMapper.selectOneData("accessKeySecret");
        String signName = smsTemplateConfigMapper.selectOneData("signName");
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = AliyunSendSms.createClient(accessKeyId, accessKeySecret);
        AliyunSendSmsDTO aliyunSendSmsDTO = new AliyunSendSmsDTO();
        aliyunSendSmsDTO.setName(name);
        log.info("发送验证码参数：signName：[{}]，templateCode：[{}]，tel：[{}]，templateParam：[{}]", signName, templateCode, phoneNum, JSON.toJSONString(aliyunSendSmsDTO, true));
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phoneNum)
                .setTemplateParam(JSON.toJSONString(aliyunSendSmsDTO));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            return sendSmsResponse.getStatusCode();
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            log.warn(error.message);
        }
        return 0;
    }

}

